package com.tuanzi.blog.service.impl;

import com.tuanzi.blog.dao.BlogMapper;
import com.tuanzi.blog.dao.TagMapper;
import com.tuanzi.blog.dao.TypeMapper;
import com.tuanzi.blog.dao.UserMapper;
import com.tuanzi.blog.model.Blog;
import com.tuanzi.blog.model.BlogWithBLOBs;
import com.tuanzi.blog.model.Tag;
import com.tuanzi.blog.service.BlogService;
import com.tuanzi.blog.util.MarkdownUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 雄关漫道真如铁，而今迈过从头越
 **/

@Service
public class BlogServiceImpl implements BlogService {

    @Resource
    private TypeMapper typeMapper;

    @Resource
    private BlogMapper blogMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private TagMapper tagMapper;

    @Override
    public BlogWithBLOBs getBlog(Long id) {
        BlogWithBLOBs blog = blogMapper.selectByPrimaryKey(id);
        blog.setType(typeMapper.selectByPrimaryKey(blog.getTypeId()));
        blog.setUser(userMapper.selectByPrimaryKey(blog.getUserId()));
        List<Tag> tagList = new ArrayList<>();
        for (String s : blog.getTags().split(",")) {
            Tag tag = tagMapper.selectByPrimaryKey(Long.parseLong(s));
            tagList.add(tag);
        }
        blog.setTagList(tagList);
        //这里我们要把content内容的Markdown格式转化为html格式
        String content = blog.getContent();
        String s = MarkdownUtils.markdownToHtmlExtensions(content);
        blog.setContent(s);
        return blog;
    }

    @Override
    public List<BlogWithBLOBs> getAllBlogs() {
        List<BlogWithBLOBs> blogs = blogMapper.selectAll();
        for (BlogWithBLOBs blogWithBLOBs : blogs) {
            blogWithBLOBs.setType(typeMapper.selectByPrimaryKey(blogWithBLOBs.getTypeId()));
            blogWithBLOBs.setUser(userMapper.selectByPrimaryKey(blogWithBLOBs.getUserId()));
        }
        return blogs;
    }

    /**
     * 指定条件查询信息
     * @return
     */
    @Override
    public List<BlogWithBLOBs> list(BlogWithBLOBs blog) {

        List<BlogWithBLOBs> list = blogMapper.getList(blog);
        for (BlogWithBLOBs blogWithBLOBs : list) {
            blogWithBLOBs.setType(typeMapper.selectByPrimaryKey(blogWithBLOBs.getTypeId()));
            blogWithBLOBs.setUser(userMapper.selectByPrimaryKey(blogWithBLOBs.getUserId()));
        }

        return list;
    }

    /**
     * 1：添加成功
     * 2：更改成功
     * 3：添加失败
     * 4：更改失败
     */
    @Override
    public int saveBlog(BlogWithBLOBs blog) {
        if (blog.getId() != null){
            blog.setUpdateTime(new Date());
            int i = blogMapper.updateByPrimaryKeyWithBLOBs(blog);
            if (i == 1){
                //获取所有表的个数
                int size = blogMapper.selectTypeCounts(blog.getTypeId());
                typeMapper.updateSize(size,blog.getTypeId());
                return 2;
            }
            return 4;
        }
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0);
        if (blogMapper.insert(blog) == 1){
            //获取所有表的个数
            int size = blogMapper.selectTypeCounts(blog.getTypeId());
            typeMapper.updateSize(size,blog.getTypeId());
            return 1;
        }
        return 3;
    }

    @Override
    public BlogWithBLOBs deleteBlog(Long id) {
        BlogWithBLOBs blog = blogMapper.selectByPrimaryKey(id);
        if (blogMapper.deleteByPrimaryKey(id) == 1){
            //获取所有表的个数
            int size = blogMapper.selectTypeCounts(blog.getTypeId());
            typeMapper.updateSize(size,blog.getTypeId());
            return blog;
        }
        return null;
    }

    /**
     * 根据类型查询博客
     * @return
     */
    @Override
    public List<BlogWithBLOBs> getTypeBlogs(Long id) {
        List<BlogWithBLOBs> blogs = blogMapper.selectByType(id);
        for (BlogWithBLOBs blogWithBLOBs : blogs) {
            blogWithBLOBs.setType(typeMapper.selectByPrimaryKey(blogWithBLOBs.getTypeId()));
            blogWithBLOBs.setUser(userMapper.selectByPrimaryKey(blogWithBLOBs.getUserId()));
        }
        return blogs;
    }

    /**
     * 查询最新推荐的六篇文章
     */
    @Override
    public List<BlogWithBLOBs> getTimeBlogs(int size) {
        List<BlogWithBLOBs> timeBlogs = blogMapper.getTimeBlogs(size);
        return timeBlogs;
    }

    /**
     * 全局搜索，内容或者标题中含有和这个字段的博客列表
     */
    @Override
    public List<BlogWithBLOBs> searchBlogByTitleOrContent(String title) {
        List<BlogWithBLOBs> blogWithBLOBs = blogMapper.searchBlogByTitleOrContent(title);
        for (BlogWithBLOBs blogWithBLOB : blogWithBLOBs) {
            blogWithBLOB.setType(typeMapper.selectByPrimaryKey(blogWithBLOB.getTypeId()));
            blogWithBLOB.setUser(userMapper.selectByPrimaryKey(blogWithBLOB.getUserId()));
        }
        return blogWithBLOBs;
    }

    /**
     * 归档查询
     * @return
     */
    @Override
    public Map<String, List<BlogWithBLOBs>> archiveBlogs() {

        List<String> years = blogMapper.getYears();

        Map<String,List<BlogWithBLOBs>> map = new HashMap<>();
        for (String year : years) {
            map.put(year,blogMapper.selectByYear(year));
        }

        return map;
    }

    /**
     * 查询博客数量
     * @return
     */
    @Override
    public Long countBlog() {
        Long aLong = blogMapper.selectCount();
        return aLong;
    }

}
